package com.xiaochong.tir.apiservice.util;

import com.xiaochong.tir.common.util.AkkaParamUtils;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author junhua
 * @date 2018/3/23 19:47
 */
public class HttpUtils {

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public final static int CONNECT_TIMEOUT = 60;

    public final static int READ_TIMEOUT = 100;

    public final static int WRITE_TIMEOUT = 60;


    public static boolean akkaFlag;

    /**
     * GET 请求
     *
     * @param url 请求路径
     * @return ResponseBody 响应
     * @throws IOException 异常
     */
    public static String get(String url) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                //设置读取超时时间
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                //设置写的超时时间
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //设置连接超时时间
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                //设置连接池大小存活时间
                .connectionPool(new ConnectionPool(50, 5L, TimeUnit.MINUTES))
                .build();
        Request build = new Request.Builder()
                .url(url)
                .build();
        Response response = null;
        try {
            response = client.newCall(build)
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String message = response.body().string();
        return message;
    }

    /**
     * GET 请求
     *
     * @param url 请求路径
     * @return ResponseBody 响应
     * @throws IOException 异常
     */
    public static String getAkka(String url) throws IOException {
        if(HttpUtils.akkaFlag){
            return HttpUtils.get(url);
        }
        return null;
    }

    @Value("${tirservice.akkaFlag}")
    public static void setAkkaFlag(boolean akkaFlag) {
        HttpUtils.akkaFlag = akkaFlag;
    }

    /**
     * POST 请求
     *
     * @param url  url
     * @param body 请求体
     * @return ResponseBody 响应
     * @throws IOException 异常
     */
    public static String post(String url, FormBody body) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                //设置读取超时时间
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                //设置写的超时时间
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //设置连接超时时间
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                //设置连接池大小存活时间
                .connectionPool(new ConnectionPool(50, 5L, TimeUnit.MINUTES))
                .build();
        Request build = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(build).execute();
        String message = response.body().string();
        return message;
    }


    /**
     * POST JSON 提交
     *
     * @param url  地址
     * @param json json
     * @return 字符串
     * @throws IOException 异常
     */
    public static String post(String url, String json) throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                //设置读取超时时间
                .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                //设置写的超时时间
                .writeTimeout(WRITE_TIMEOUT, TimeUnit.SECONDS)
                //设置连接超时时间
                .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                //设置连接池大小存活时间
                .connectionPool(new ConnectionPool(50, 5L, TimeUnit.MINUTES))
                .build();
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        }
    }

}
